var t = require("../../../../@babel/runtime/helpers/defineProperty"), s = getApp();

Component({
    properties: {
        goodsInfo: {
            type: Object
        },
        cartList: {
            type: Object
        }
    },
    data: {
        language: "",
        optionsId: 0,
        cartid: 0,
        goodsCount: 1,
        optionsMaxCount: 1,
        popupSpecs1: !1,
        popupSpecs2: !1,
        popThumb: "",
        popStock: 0,
        popPrice: 0,
        specsManage: [],
        goodsDescription: "",
        popupModeshow: !0,
        skuImages: [],
        showSkuImages: !1,
        activeSkuIndex: 0,
        activeSkuIndex_1: "",
        activeSkuIndex_2: ""
    },
    lifetimes: {
        attached: function() {
            var t = wx.getStorageSync("langIndex");
            this.setData({
                language: t.en,
                specsManage: [],
                optionsId: 0,
                popupSpecs1: !0
            }), this.initPopView();
        },
        detached: function() {}
    },
    methods: {
        submitAction: function() {
            1 != this.data.goodsInfo.has_option || this.data.optionsId ? this.triggerEvent("closeSpecsPopup", {
                optionsId: this.data.optionsId,
                cartid: this.data.cartid,
                goodsId: this.data.goodsInfo.id,
                goodsCount: this.data.goodsCount,
                clicktype: 1
            }) : wx.showToast({
                icon: "none",
                title: "规格id不可为空，请选择规格！",
                duration: 1500
            });
        },
        popupSpecsbtn: function() {
            this.setData({
                popupSpecs1: !1,
                popupModeshow: !0
            }), this.triggerEvent("closeSpecsPopup", {
                clicktype: 0
            });
        },
        initPopView: function() {
            var t = this;
            if (1 == this.data.goodsInfo.has_option) {
                this.setData({
                    popPrice: this.data.goodsInfo.min_price + "-" + this.data.goodsInfo.max_price,
                    popThumb: this.data.goodsInfo.thumb,
                    popStock: this.data.goodsInfo.stock,
                    goodsDescription: "选择"
                });
                for (var s = 0; s < this.data.goodsInfo.has_many_specs.length; s++) this.setData({
                    goodsDescription: this.data.goodsDescription + " " + this.data.goodsInfo.has_many_specs[s].title
                });
                var o = [];
                if (this.data.goodsInfo.has_many_options.forEach(function(s, a) {
                    var i = "" != s.thumb ? s.thumb : t.data.goodsInfo.thumb;
                    o.push(i);
                }), this.setData({
                    skuImages: o
                }), !this.data.optionsId) for (var a = 0; a < this.data.goodsInfo.has_many_specs.length; a++) this.data.goodsInfo.has_many_specs[a].description = this.data.goodsInfo.has_many_specs[a].specitem[0].id, 
                this.selectSpecs(this.data.goodsInfo.has_many_specs[a].specitem[0], "first", a);
            } else {
                console.log("单规格", this.data.goodsInfo);
                var i = [];
                i.push(this.data.goodsInfo.thumb), this.setData({
                    popThumb: this.data.goodsInfo.thumb,
                    popStock: this.data.goodsInfo.stock,
                    popPrice: this.data.goodsInfo.price,
                    goodsDescription: "",
                    optionsMaxCount: this.data.goodsInfo.stock,
                    skuImages: i
                });
                var n = this.data, e = n.cartList, d = n.goodsInfo;
                if (e && e.length > 0) for (var h = 0; h < e.length; h++) {
                    if (e[h].goods_id === d.id) {
                        this.setData({
                            goodsCount: e[h].total,
                            cartid: e[h].id
                        });
                        break;
                    }
                    this.data.cartid = 0;
                }
            }
        },
        inputGoodsNum: function(t) {
            var o = parseInt(t.detail.value), a = this.data.goodsCount;
            8 == t.detail.keyCode && s._isTextEmpty(t.detail.value) ? this.setData({
                goodsCount: ""
            }) : o ? this.setData({
                goodsCount: o
            }) : this.setData({
                goodsCount: a
            });
        },
        reduceGoods: function() {
            var t = this.data.goodsCount;
            1 != this.data.goodsCount && 0 != this.data.goodsCount && this.setData({
                goodsCount: t - 1
            });
        },
        addGoods: function() {
            var t = this.data.goodsCount, s = this.data.specsManage, o = this.data.optionsMaxCount;
            if (console.log("增加"), s.length == this.data.goodsInfo.has_many_specs.length && this.data.goodsCount == o) return console.log("数量超出范围"), 
            wx.showToast({
                icon: "none",
                title: "数量超出范围",
                duration: 1500
            }), void console.log("max=" + this.data.goodsCount);
            this.setData({
                goodsCount: t + 1
            });
        },
        selectSpecs: function(s, o, a) {
            var i = {}, n = 0;
            if ("first" == o ? (n = a, i = s) : "sku_swiper" == o ? i = s : (n = s.currentTarget.dataset.specsidex, 
            i = s.currentTarget.dataset.specitem), console.log(i), i.c) return !1;
            this.setData(t({}, "goodsInfo.has_many_specs[" + n + "].description", i.id)), this.manageSpecs(i), 
            this.setGoodsSpecs(i), this.setGoodsSpecsChangeInfo(), this.getMaxCount();
        },
        manageSpecs: function(t) {
            var s = new Object(), o = this.data.specsManage;
            if (s.id = t.id, s.specid = t.specid, s.title = t.title, o.length > 0) {
                for (var a = 0; a < o.length; a++) o[a].specid == s.specid && o.splice(a, 1);
                o.push(s);
            } else o.push(s);
            if (o.length == this.data.goodsInfo.has_many_specs.length) {
                for (var i = [], n = 0; n < this.data.goodsInfo.has_many_specs.length; n++) for (var e = 0; e < o.length; e++) if (this.data.goodsInfo.has_many_specs[n].id == o[e].specid) {
                    i.push(o[e]);
                    break;
                }
                o = i;
            }
            this.setData({
                specsManage: o
            }), this.setGoodsDescription();
        },
        setGoodsDescription: function() {
            var t = "", s = this.data.specsManage;
            if (s.length == this.data.goodsInfo.has_many_specs.length) {
                t = "已选择 ";
                for (var o = 0; o < s.length; o++) t += s[o].title + " ";
                this.setData({
                    goodsDescription: t
                });
            } else {
                t = "请选择 ";
                for (var a = 0; a < this.data.goodsInfo.has_many_specs.length; a++) for (var i = 0; i < s.length; i++) if (this.data.goodsInfo.has_many_specs[a].id != s[i].specid) {
                    t += this.data.goodsInfo.has_many_specs[a].title + " ";
                    break;
                }
                this.setData({
                    goodsDescription: t
                });
            }
        },
        setGoodsSpecs: function(t) {
            for (var s = 0; s < this.data.goodsInfo.has_many_specs.length; s++) t.specid, this.data.goodsInfo.has_many_specs[s].id;
        },
        setGoodsSpecsStatus: function(t, s) {
            for (var o = [], a = 0; a < this.data.goodsInfo.has_many_options.length; a++) for (var i = this.data.goodsInfo.has_many_options[a].specs.split("_"), n = 0; n < i.length; n++) if (i[n] == s) return void o.push(this.data.goodsInfo.has_many_options[a]);
            for (var e = 0; e < o.length; e++) for (var d = o[e].specs.split("_"), h = 0; h < d.length; h++) if (d[h] != s && 0 == o[e].stock) for (var p = 0; p < t.length; p++) d[h] == t[p].id && (t[p].c = !0); else if (d[h] != s && o[e].stock > 0) for (var c = 0; c < t.length; c++) d[h] == t[c].id && (t[c].c = !1);
            return t;
        },
        setGoodsSpecsChangeInfo: function() {
            var t = this.data.specsManage;
            if (t.sort(function(t, s) {
                return t.id - s.id;
            }), t.length == this.data.goodsInfo.has_many_specs.length) {
                for (var o = "", a = 0; a < t.length; a++) o += t[a].id + "_";
                o = o.substring(0, o.length - 1);
                for (var i = 0; i < this.data.goodsInfo.has_many_options.length; i++) if (o == this.setGoodsSpecsBySort(this.data.goodsInfo.has_many_options[i].specs)) {
                    this.setData({
                        activeSkuIndex: i,
                        popPrice: this.data.goodsInfo.has_many_options[i].product_price,
                        popThumb: s._isTextEmpty(this.data.goodsInfo.has_many_options[i].thumb) ? this.data.goodsInfo.thumb : this.data.goodsInfo.has_many_options[i].thumb,
                        popStock: this.data.goodsInfo.has_many_options[i].stock,
                        optionsId: this.data.goodsInfo.has_many_options[i].id
                    }), this.data.optionsMaxCount = this.data.goodsInfo.has_many_options[i].stock, this.data.optionsMaxCount > 0 && this.setData({
                        goodsCount: 1
                    });
                    break;
                }
            }
            this.setData({
                specsManage: t
            });
        },
        setGoodsSpecsBySort: function(t) {
            var s = t.split("_");
            s.sort(function(t, s) {
                return t - s;
            });
            for (var o = "", a = 0; a < s.length; a++) o += s[a] + "_";
            return o = o.substring(0, o.length - 1);
        },
        getMaxCount: function() {
            var t = this.data.specsManage, s = this.data.optionsMaxCount;
            console.log(t, s, this.data.optionsId), t.length == this.data.goodsInfo.has_many_specs.length && (0 == s && this.setData({
                goodsCount: 0
            }), this.data.goodsCount > s && this.setData({
                goodsCount: s
            }));
            var o = this.data, a = o.cartList, i = o.goodsInfo, n = o.optionsId;
            if (console.log("666", a), a && a.length > 0) for (var e = 0; e < a.length; e++) {
                if (a[e].goods_id === i.id && a[e].option_id === n) {
                    console.log(a[e]), this.setData({
                        goodsCount: a[e].total,
                        cartid: a[e].id
                    });
                    break;
                }
                this.data.cartid = 0;
            }
        },
        showSkuImages_Methob: function() {
            this.setData({
                popupSpecs1: !1,
                popupSpecs2: !1,
                popupModeshow: !0,
                showSkuImages: !0
            }), 1 == this.data.goodsInfo.has_option && this.setData({
                activeSkuIndex_1: this.data.goodsInfo.has_many_options[this.data.activeSkuIndex].specs
            });
        },
        onChangeSkuImagesIndex: function(t) {
            var s = t.detail.current;
            this.setData({
                activeSkuIndex: s
            }), 1 == this.data.goodsInfo.has_option && this.setData({
                activeSkuIndex_2: this.data.goodsInfo.has_many_options[s].specs
            });
        },
        onCloseSkuImages: function() {
            if (1 == this.data.goodsInfo.has_option) {
                var t = this.data.activeSkuIndex_1.split("_"), o = this.data.goodsInfo, a = this.data.activeSkuIndex;
                if (this.data.activeSkuIndex_1 != this.data.goodsInfo.has_many_options[a].specs) for (var i = this.data.activeSkuIndex_2.split("_"), n = 0; n < t.length; n++) if (t[n] != i[n]) for (var e = Number(i[n]), d = 0; d < this.data.goodsInfo.has_many_specs.length; d++) for (var h = 0; h < this.data.goodsInfo.has_many_specs[d].specitem.length; h++) this.data.goodsInfo.has_many_specs[d].specitem[h].id == e && (o.has_many_specs[d].description = this.data.goodsInfo.has_many_specs[d].specitem[h].id);
                console.log(this.data.activeSkuIndex_1, "that.data.activeSkuIndex_1"), this.setData({
                    goodsInfo: o,
                    showSkuImages: !1,
                    popupSpecs1: !0,
                    popupModeshow: !1,
                    popPrice: this.data.goodsInfo.has_many_options[a].product_price,
                    popThumb: s._isTextEmpty(this.data.goodsInfo.has_many_options[a].thumb) ? this.data.goodsInfo.thumb : this.data.goodsInfo.has_many_options[a].thumb,
                    popStock: this.data.goodsInfo.has_many_options[a].stock,
                    optionsId: this.data.goodsInfo.has_many_options[a].id
                });
            } else this.setData({
                popupSpecs2: !0,
                popupModeshow: !1,
                showSkuImages: !1
            });
        }
    }
});